package com.gxa.medical.controller;

import com.gxa.medical.common.BizException;
import com.gxa.medical.common.R;
import com.gxa.medical.entity.dto.UseLoginDto;
import com.gxa.medical.entity.pojo.User;
import com.gxa.medical.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 *RequestMapping:前端访问这个控制器的方式
 * http://localhost:8080/user
 *
 *RestController:交给Spring管理的一个bean
 */
@RequestMapping("/user")
@RestController
@Api(tags = "用户登录注册相关接口")
public class UserController {

//    在游览器直接访问http://localhost:8080/user/login
//    @GetMapping("/login")
//    public String login(){
//        return "hello world";
//    }
    @Autowired
    private UserService userService;
    @ApiOperation("登录接口")
    @PostMapping("/login")
    public R login(@Valid @RequestBody UseLoginDto dto)  throws BizException {
      //从Spring容器中找到UserService对象并注入userService
        //select * frong tb_user where phone=? and pwd=?
        User login = userService.login(dto.getPhone(), dto.getPwd());
        //有可能发生空指针异常
        login.setUpdated(new Date());

        return R.success();

//        if(login != null){
//            return R.success();
//        }else{
//            return R.failed("登录失败,请检查用户名或密码");
//        }

    }
    @ApiOperation("注册接口")
    @PostMapping("/register")
    public R register(@RequestBody User user) throws Exception {

        System.out.println("从前端接收的信息："+user);
        user.setCreated(new Date());

        //注册成功后返回User,此时user中含有id特征
        User register = userService.register(user);

        return R.success(register);
    }
    @ApiOperation("查询全部用户信息列表")
    @GetMapping("/list")
    public R list(){
        List<User> list = userService.list();
        return R.success(list);
    }



}
